<template>
  <view class="container" style="margin-top: 100rpx">
    <!-- 当前店铺 -->
    <view class="current-store">
      <text class="section-title">当前店铺</text>
      <view class="store-card">
        <text class="store-name">{{ currentStore?.shopName }}</text>
      </view>
    </view>

    <!-- 店铺列表 -->
    <view class="store-list">
      <text class="section-title">我的店铺</text>
      <view v-for="store in stores" :key="store.merchantId" class="store-item">
        <text class="store-name">{{ store.shopName }}</text>
        <button
            class="switch-btn"
            @tap="openDialog(store)"
            hover-class="button-hover"
        >切换</button>
      </view>
    </view>

    <!-- 弹窗 -->
    <view v-if="showDialog" class="mask">
      <view class="dialog">
        <text class="dialog-title">确认切换店铺？</text>
        <text class="dialog-content">当前选择的店铺：{{ selectedStore?.shopName }}</text>
        <view class="dialog-btns">
          <button class="btn cancel" @tap="showDialog = false">取消</button>
          <button class="btn confirm" @tap="confirmChange">确认</button>
        </view>
      </view>
    </view>
  </view>
</template>

<script setup>
import { ref, reactive, onMounted} from 'vue'

const stores = ref([])
onMounted( ()=>{
  console.log(uni.getStorageSync("user_me"));
  stores.value =  uni.getStorageSync("user_me");
  currentStore.value=stores.value[0];
  uni.setStorageSync("currentStore", currentStore.value)
})
const getstore = async()=>{
  stores.value = await uni.getStorageSync("user_me");
}

const currentStore = ref()
const showDialog = ref(false)
const selectedStore = ref(null)

const openDialog = (store) => {
  selectedStore.value = store
  showDialog.value = true
}

const confirmChange = () => {
  currentStore.value = selectedStore.value
  showDialog.value = false
  uni.setStorageSync("currentStore", currentStore.value)
  console.log(uni.getStorageSync("currentStore"));
}
</script>

<style lang="scss">
.container {
  padding: 32rpx;
  min-height: 100vh;
  background-color: #f5f5f5;
}

.section-title {
  font-size: 32rpx;
  color: #333;
  margin-bottom: 24rpx;
  display: block;
  font-weight: bold;
}

.current-store {
  margin-bottom: 48rpx;

  .store-card {
    background-color: #fff;
    border-radius: 16rpx;
    padding: 32rpx;

    .store-name {
      font-size: 36rpx;
      color: #007AFF;
    }
  }
}

.store-list {
  .store-item {
    background-color: #fff;
    border-radius: 16rpx;
    padding: 28rpx 32rpx;
    margin-bottom: 24rpx;
    display: flex;
    align-items: center;
    justify-content: space-between;

    .store-name {
      font-size: 30rpx;
      color: #333;
    }
  }
}

.switch-btn {
  width: 120rpx;
  height: 56rpx;
  line-height: 56rpx;
  background-color: #007AFF;
  color: #fff;
  font-size: 26rpx;
  border-radius: 8rpx;
  margin: 0;

  &::after {
    border: none;
  }

  &.button-hover {
    opacity: 0.8;
  }
}

.mask {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0,0,0,0.5);
  display: flex;
  align-items: center;
  justify-content: center;
}

.dialog {
  width: 600rpx;
  background: #fff;
  border-radius: 24rpx;
  padding: 48rpx 32rpx;

  .dialog-title {
    font-size: 32rpx;
    color: #333;
    text-align: center;
    display: block;
    margin-bottom: 24rpx;
    font-weight: bold;
  }

  .dialog-content {
    font-size: 28rpx;
    color: #666;
    text-align: center;
    display: block;
    margin-bottom: 48rpx;
  }
}

.dialog-btns {
  display: flex;
  justify-content: space-between;

  .btn {
    width: 240rpx;
    height: 80rpx;
    line-height: 80rpx;
    border-radius: 12rpx;
    font-size: 30rpx;

    &.cancel {
      background-color: #f5f5f5;
      color: #666;
    }

    &.confirm {
      background-color: #007AFF;
      color: #fff;
    }
  }
}
</style>